package com.gzr.controller;

import com.gzr.pojo.Category;
import com.gzr.pojo.Result;
import com.gzr.server.CategoryServer;
import jakarta.websocket.server.PathParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.List;

@RestController
@RequestMapping("/category")
public class CategoryController {

    @Autowired
    private CategoryServer categoryServer;

    /**
     * 新增文章分类
     * @param category
     * @return
     */
    @PostMapping
    public Result add(@RequestBody @Validated Category category) {
        category.setCreateTime(new Date());
        category.setUpdateTime(new Date());
        categoryServer.add(category);
        return Result.success();
    }

    /**
     * 查询文章分类
     * @return
     */
    @GetMapping
    public Result<List<Category>> categoryList() {
        List<Category>  categoryList = categoryServer.categoryList();
        return Result.success(categoryList);
    }

    /**
     * 获取文章分类详情
     * @param id
     * @return
     */
    @GetMapping("/detail")
    public Result<Category> detail(Integer id) {
        Category category = categoryServer.findById(id);
        return Result.success(category);
    }

    /**
     * 更新文章分类
     * @param category
     * @return
     */
    @PutMapping
    public Result updateCategory(@RequestBody @Validated(value = Category.Update.class) Category category) {
         categoryServer.update(category);
         return Result.success();
    }

    /**
     * 删除文章分类
     * @param id
     * @return
     */
    @DeleteMapping
    public Result deleteCategory(@RequestParam("id") Integer id) {
        Category category = new Category();
        category.setId(id);
        categoryServer.delete(category);
        return Result.success();
    }
}
